interface Player {
  bankerCount: number,
  lftId: number,
  nickname: string,
  owner: boolean,
  point: number,
  seatIndex: number,
  suitDescribe: Array<{name: string, count: number}>,
  suitMap: any,
  suitPatterns: any,
  uid: string,
  winner: boolean,
  headUrl: string
}

declare var window: any

class UserLabel extends eui.Component {
  private userGroup: eui.Group
  private userContentCaption: eui.Group
  private useridGroup: eui.Group
  private userContent: eui.Group
  private userScore: eui.Group
  private username: eui.Label
  private userid: eui.Label
  private highest: eui.Label
  private boom: eui.Label
  private winlose: eui.Label
  private zhanjiText: eui.Image
  public player: Player
  public idFrame: eui.Image
  public headUrl: eui.Image
  public mask: eui.Image
  public headUrlFrame: eui.Image
  constructor(player) {
    super()
    this.once(eui.UIEvent.COMPLETE, this.onComplete, this)
    this.player = player
    this.skinName = `<e:Skin class="JinhuaSkin" width="720" height="244" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
      <e:Group verticalCenter="0" horizontalCenter="0" id="userGroup" width="720" height="264">
        <e:Image id="headUrlFrame" x="86" y="30" width="80" height="72"></e:Image>
        <e:Image id="headUrl" x="91" y="34" width="70" height="62"></e:Image>
        <e:Image id="mask" x="91" y="34" width="70" height="62"></e:Image>
        <e:Label id="username" text="" textColor="0xbd2c00" size="29" x="182" y="36" height="43" width="185"></e:Label>
        <e:Group id="useridGroup" x="182" y="70">
         <e:Image id="idFrame" x="122" y="38" width="22" height="22"></e:Image>
          <e:Label id="userid" text="" size="27" height="43"></e:Label>
        </e:Group>
        <e:Group id="userContentCaption" x="91" y="120" width="132" height="172" >
          <e:Label text="单局最高分" textColor="0x9c6318"></e:Label>         
          <e:Label text="打出炸弹数" textColor="0x9c6318"></e:Label>       
          <e:Label text="胜负局数" textColor="0x9c6318"></e:Label>         
        </e:Group>
        <e:Group id="userContent" x="261" y="120" width="132" height="172">
          <e:Label id="highest"></e:Label>        
          <e:Label id="boom"></e:Label>       
          <e:Label id="winlose"></e:Label>        
        </e:Group>
        <e:Image id="zhanjiText" y="52" right="88"></e:Image>
        <e:Group id="userScore" right="23" y="112" width="260" height="96">
        </e:Group>
      </e:Group>
    </e:Skin>`
  }

  scoreNumber(point) {
    const pointSigned = point > 0 ? '+' + point : point
    const numbers = (pointSigned + '').split('')
    return numbers.map(num => {
      if (point >= 0) {
        if (num === '+') {
          return 'pdk-jia-active_png'
        }
        return `pdk-${num}_png`
      } else {
        if (num === '-') {
          return 'pdk-jian-default_png'
        }
        return `pdk-1${num}_png`
      }
    })
  }
  private onComplete(e: eui.UIEvent) {
    const player = this.player

    var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
    hLayout.gap = 10
    hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
    this.useridGroup.layout = hLayout   /// 水平布局

    var vLayout:eui.VerticalLayout = new eui.VerticalLayout()
    vLayout.gap = 8
    var vLayout2:eui.VerticalLayout = new eui.VerticalLayout()
    vLayout2.horizontalAlign = egret.HorizontalAlign.LEFT
    vLayout2.gap = 8
    this.userContent.layout = vLayout //设置问垂直布局
    this.userContentCaption.layout = vLayout2 //设置问垂直布局
    console.error(player)
    this.highest.text = `${player.suitMap.maxPoint}`
    this.boom.text = ` ${player.suitMap.zhaDanCount}`
    this.winlose.text = ` ${player.suitMap.winCount}赢 ${player.suitMap.loseCount}输`

    this.username.text = this.player.nickname
    this.userid.text = 'ID ' + this.player.lftId
    if (this.player.winner) {
      var winnerBg = new eui.Image('faguang_win_png')
      winnerBg.x = 60
      winnerBg.y = 0
      winnerBg.width = 600
      winnerBg.height = 244
      this.userGroup.addChildAt(winnerBg, 0)
      var winner = new eui.Image('pdk-winner_png')
      winner.right = 78
      winner.top = 3
      this.userGroup.addChild(winner)
    } else {
      var loserBg = new eui.Image('faguang_lose_png')
      loserBg.x = 60
      loserBg.y = 0
      loserBg.width = 600
      loserBg.height = 244
      this.userGroup.addChildAt(loserBg, 0)
    }



    // var ico:loadImage = new loadImage(msg.headUrl,this.imgMask.width); 
    //     ico.x = this.imgMask.x;
    //     ico.y = this.imgMask.y; 
    //     this.addChild(ico); 
    //     this.setChildIndex(ico,3)
    //     ico.mask = this.imgMask;  

    if (this.player.owner) {
      var owner = new eui.Image('owner_niuniu_png')
      owner.right = 66
      owner.top = 4
      this.userGroup.addChild(owner)
    }
    if (this.player.winner && this.player.owner) {
      winner.right = 112
      owner.right = 60
      owner.top = 6
    }

    const yellowColor = 0xbd2c00
    this.userid.textColor = 0x9c6318
    // if (player.point >= 0) {
      // this.zhanjiText.source = 'pdk-active-total_png'
    this.highest.textColor = yellowColor
    this.boom.textColor = yellowColor
    this.winlose.textColor = yellowColor
      // this.username.textColor = 0xDC143C
    // } else {
      // this.zhanjiText.source = 'pdk-default-total_paodekuai_png'      
    // }
    this.idFrame.source = 'id-frame_png'
    this.mask.source = 'mask_png'
    this.headUrlFrame.source = 'headurl-frame_png'
    if(this.player.headUrl) {
      if (window.location.href.match('10.10.10')) {
        this.headUrl.source = this.player.headUrl.replace('./', 'https://c-c.jiahuagame.com/console/')
      } else {
        this.headUrl.source = this.player.headUrl
      }
    } else {
      
    }
    this.headUrl.mask = this.mask
    var hLayoutScore:eui.HorizontalLayout = new eui.HorizontalLayout()
    hLayoutScore.gap = 0
    hLayoutScore.paddingRight = 30
    hLayoutScore.horizontalAlign = egret.HorizontalAlign.CENTER
    this.userScore.layout = hLayoutScore   /// 水平布局

    const scoreNumber = this.scoreNumber(this.player.point)
    scoreNumber.forEach(number => {
      let numImg = new eui.Image(number)
      numImg.width = 40
      numImg.height = 62
      this.userScore.addChild(numImg)
    })

    if (this.$hasAddToStage) {
      window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
    } else {
      this.once(eui.UIEvent.ADDED_TO_STAGE, function () {
        setTimeout(() => {
          window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
        }, window.labelRenderTime)
      }, this)
    }
  }
}

export default UserLabel